package com.kelvin.paopao.module.main.date.mydate.myask;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kelvin.paopao.R;
import com.kelvin.paopao.adapter.MyAskDateAdapter;
import com.kelvin.paopao.adapter.MyDateAdapter;
import com.kelvin.paopao.api.responseBean.DateListModel;
import com.kelvin.paopao.injector.components.DaggerMyAskDateComponent;
import com.kelvin.paopao.injector.modules.MyAskDateModule;
import com.kelvin.paopao.module.base.BaseFragment;
import com.kelvin.paopao.module.main.date.dateperson.DatePersonActivity;
import com.kelvin.paopao.module.person.detail.PersonDetailActivity;
import com.kelvin.paopao.module.person.photoset.PhotoSetActivity;
import com.kelvin.paopao.widgets.EmptyLayout;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by MCK on 2017/5/17.
 */

public class MyAskDateFragment extends BaseFragment<IMyAskDatePresenter>
        implements IMyAskDateView {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.empty_layout)
    EmptyLayout emptyLayout;

    @Inject
    MyAskDateAdapter adapter;
    private LinearLayoutManager manager;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_my_publish_date;
    }

    @Override
    protected void initInjector() {
        DaggerMyAskDateComponent.builder().myAskDateModule(new MyAskDateModule(this))
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<String> urls = new ArrayList<String>();
                urls.add(dateListModelList.get(position).getImg1());
                urls.add(dateListModelList.get(position).getImg2());
                urls.add(dateListModelList.get(position).getImg3());
                if (view.getId() == R.id.iv_content1) {
                    PhotoSetActivity.launch(getContext(), urls, 0);
                }
                if (view.getId() == R.id.iv_content2) {
                    PhotoSetActivity.launch(getContext(), urls, 1);
                }
                if (view.getId() == R.id.iv_content3) {
                    PhotoSetActivity.launch(getContext(), urls, 2);
                }
                if (view.getId() == R.id.iv_avatar) {
                    PersonDetailActivity.launch(getContext(), dateListModelList.get(position).getUserno());
                }
            }
        });
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        mPresenter.getData(isRefresh);
    }

    private List<DateListModel> dateListModelList;

    @Override
    public void loadData(List<DateListModel> myModelList) {
        this.dateListModelList = myModelList;
        adapter.setNewData(myModelList);
    }

    @Override
    public void showNodata() {
        View nodataView = LayoutInflater.from(getContext()).inflate(R.layout.view_no_data,
                (ViewGroup) recyclerView.getParent(), false);
        adapter.setEmptyView(nodataView);
    }

    public static MyAskDateFragment newInstance() {
        Bundle args = new Bundle();

        MyAskDateFragment fragment = new MyAskDateFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
